<?php
namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Api\BaseController;
use App\Models\Api\User; //测试
use App\Models\Divination;
use App\Models\Stage;
use App\Models\Api\Tarot;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use DB;

class TarotController extends BaseController
{

/**
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 * 塔罗结果
 */
    public function add(Request $request)
    {
        $question = $request->input('content');
        if (empty($question)) {
            return show(config('code.error'), '请描述问题~');
        }
        $result =contentCensor($question);
       if ($result['status'] === 2) {
           // \Log::info('占卜错误:' . $validate->getError());
            return show(config('code.error'), $result['msg']);
        }
        $tarots = Tarot::get()->toArray();
        shuffle($tarots);
        $tarot = array_slice($tarots, 0, 3);
        $data   = [
            'type_id'   => 3,
            'user_id'   => $request->uid,
            'problem'   => $question,
            'before_id' => $tarot[0]['id'],
            'now_id'    => $tarot[1]['id'],
            'future_id' => $tarot[2]['id'],
            'created_at' => time(),
        ];
        $divi              = Divination::create($data);
        return show(200, '加载成功', ['id' => intval($divi->id), 'time' => time()]);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 塔罗详细解释
     */
    public function detail(Request $request)
    {
        $id          = $request->input('id');
        $tarot  = Divination::where(['id' => $id])->first(['id','problem','created_at','before_id', 'now_id', 'future_id'])->toArray();
        $tarot_one   = Tarot::where(['id' => $tarot['before_id']])->first()->toArray();
        $tarot_one_key = mb_strpos($tarot_one['interpretation'], '★关键语：');
        $tarot_one_part1 = trim(mb_substr($tarot_one['interpretation'], 0, $tarot_one_key));
        $tarot_one_part2 = trim(mb_substr($tarot_one['interpretation'], $tarot_one_key + mb_strlen('★关键语：')));
        $tarot_one['interpretation'] = strip_tags($tarot_one_part1);
        $tarot_one['desc'] = strip_tags($tarot_one_part2);

        $tarot_two   = Tarot::where(['id' => $tarot['now_id']])->first()->toArray();
        $tarot_two_key = mb_strpos($tarot_two['interpretation'], '★关键语：');
        $tarot_two_part1 = trim(mb_substr($tarot_two['interpretation'], 0, $tarot_two_key));
        $tarot_two_part2 = trim(mb_substr($tarot_two['interpretation'], $tarot_two_key + mb_strlen('★关键语：')));
        $tarot_two['interpretation'] = strip_tags($tarot_two_part1);
        $tarot_two['desc'] = strip_tags($tarot_two_part2);

        $tarot_three = Tarot::where(['id' => $tarot['future_id']])->first()->toArray();
        $tarot_three_key = mb_strpos($tarot_three['interpretation'], '★关键语：');
        $tarot_three_part1 = trim(mb_substr($tarot_three['interpretation'], 0, $tarot_three_key));
        $tarot_three_part2 = trim(mb_substr($tarot_three['interpretation'], $tarot_three_key + mb_strlen('★关键语：')));
        $tarot_three['interpretation'] = strip_tags($tarot_three_part1);
        $tarot_three['desc'] = strip_tags($tarot_three_part2);
        $data = [
            'question'    =>$tarot['problem'],
            'time'        =>$tarot['created_at'],
            'tarot_one'   => $tarot_one,
            'tarot_two'   => $tarot_two,
            'tarot_three' => $tarot_three,
           // 'divi_id'     => $id,
        ];
        return show(config('code.success'), '加载成功', $data);
    }

   public function history(Request $request){
    $uid = $request->uid;
    $page = $request->input('page', 1);
    $perPage = 10; // Set the number of records per page

    $tarot = Divination::where('user_id', $uid)
        ->orderBy('created_at', 'desc') // Order by created_at, adjust as needed
        ->paginate($perPage);

    $data = $tarot->map(function ($item) {
        $tarotOne = Tarot::where('id', $item['before_id'])->first()->toArray();
        $tarotTwo = Tarot::where('id', $item['now_id'])->first()->toArray();
        $tarotThree = Tarot::where('id', $item['future_id'])->first()->toArray();

        return [
            'id'          =>$item['id'],
            'question'    => $item['problem'],
            'time'        => $item['created_at'],
            'tarot_one'   => $tarotOne,
            'tarot_two'   => $tarotTwo,
            'tarot_three' => $tarotThree,
        ];
    });

    return show(config('code.success'), '加载成功', $data);
}

    public function guide(){
     $data = DB::table('app_guide')->where('type',2)->get(['id','question'])->toArray();
     shuffle($data);
     $list = array_slice($data, 0, 5);
     return show(200, '成功', $list);
   }
    /**
     * @title 删除占卜历史
     * @method DELETE
     * @param int $id 记录id
     * @readme 塔罗牌
     * @return \think\Response
     */
    public function delete(Request $request)
    {
    //   if (!$request->isMethod('delete'))
    //     {
    //         return show(config('code.error'), '未知操作', [], 403);
    //     }
        $uid = intval($request->uid);

        $did = $request->input('id');

        if ($did <= 0 || $uid <= 0) {
            return show(config('code.error'), '参数错误');
        }
        $n_data = Divination::where(['id'=>$did])->first();
        if (empty($n_data))
        {
            return show(config('code.error'), '不存在该数据', [], 403);
        }
        Divination::destroy(['uid' => $uid, 'id' => $did]);

        return show(config('code.success'), '删除成功');
    }
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 塔罗咨询
     */
    public function consult(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            return show(config('code.error'), 'id不能为空哦~');
        }
        $divi   = Divination::with('before', 'now', 'future')->where(['id' => $id])->first();
        $stages = Stage::where('type', 2)->get();
        $user   = User::where(['id' => $request->uid])->first(['id', 'nickname', 'avatar', 'ticket', 'tel']);
        $data   = [
            'divi'   => $divi,
            'stages' => $stages,
            'user'   => $user,
        ];
        return show(200, '加载成功', $data);
    }

}
